//
//  CLBaseViewController.m
//  Charles
//
//  Created by Charles on 2017/2/7.
//  Copyright © 2017年 charles. All rights reserved.
//

#import "CLBaseViewController.h"
#import "CLPointerFunction.h"
#import "CLBlockRealize.h"
#import "CLAlgorithmTest.h"
#import "NSInvocationTest.h"
#import "PerformSelectorTest.h"

@interface CLBaseViewController ()


@end

@implementation CLBaseViewController

- (void)dealloc{
    NSLog(@"%@ dealloc",NSStringFromClass(self.class));
}

-(NSString *)uuid {
    CFUUIDRef puuid = CFUUIDCreate( nil );
    CFStringRef uuidString = CFUUIDCreateString( nil, puuid );
    NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
    CFRelease(puuid);
    CFRelease(uuidString);
    return result;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setBackBtn];
    
    // 测试C函数
    //testStruct();
    //returnPointerFunction();
    
    // 测试最简单的block 实现
    //simpleblk();
    
    // 测试持有变量的blcok 实现
    //variableblk();
    
    // 测试可以修改持有变量的block 实现
    //modifyvariableblk();
    
    //[self setUpRequest];
    
    // 常用算法学习
    // [CLAlgorithmTest testBinarySearch];
}

- (void)setUpRequest {
    
    NSString *mId = [HBDevice uuid];
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *currentDateStr = [formatter stringFromDate:[NSDate date]];
    
    HBDevice *device = [[HBDevice alloc]init];
    device.mID = mId;
    device.mTimes = currentDateStr;
    device.mRemarks = @"tt";
    
    NSMutableDictionary * dict = (NSMutableDictionary *)[MTLJSONAdapter JSONDictionaryFromModel:device error:nil];
    
    [CLLSaveEquipRequest uploadDataWithParams:dict suffix:nil mimeType:nil success:^(NSURLSessionDataTask *task, id responseObject, CLNetWorkResult *result) {
        [self loginRequestWithUUID:mId];

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (void)loginRequestWithUUID:(NSString *)mId {
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithCapacity:0];
    [params setObject:mId forKey:@"mId"];
    [params setObject:@"T0001" forKey:@"mUserName"];
    [params setObject:@"7+2H8MJGGCg=" forKey:@"mPassWord"];
    
    [CLLoginRequest requestDataWithMethod:CLRequestMethodPOST params:params success:^(NSURLSessionDataTask *task, id responseObject, CLNetWorkResult *result) {
        CLUser *user = result.result[0];
        NSLog(@"result is %@",user);
        if (result.token) {
            [self queryRequest:result.token]; 
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];

}


- (void)queryRequest:(NSString *)token {
    
    NSMutableDictionary * params2 = [[NSMutableDictionary alloc]initWithCapacity:0];
    [params2 setObject:token forKey:kToken];
    [params2 setObject:@(1) forKey:@"ys"];
    [params2 setObject:[NSNull null] forKey:@"key"];
    [CLLQueryRequest requestDataWithMethod:CLRequestMethodGET params:params2 success:^(NSURLSessionDataTask *task, id responseObject, CLNetWorkResult *result) {
        NSArray *array = result.result[0];
        NSLog(@"result is %@",array);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];

}


- (void)setBackBtn {
    
    NSInteger vcCount = [self.navigationController.viewControllers count];
    if (vcCount > 1) {
        UIButton * btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
        btnBack.frame = CGRectMake(0, 0, 40, 40);
        [btnBack setImage:[UIImage imageNamed:@"icon_fanhui"] forState:UIControlStateNormal];
        [btnBack addTarget:self action:@selector(backEvent:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem * backBarItem = [[UIBarButtonItem alloc]initWithCustomView:btnBack];
        self.navigationItem.leftBarButtonItem = backBarItem;
    }

}

- (void)backEvent:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)backGestureAction{
    
}

- (void) setDataArray:(NSArray *)dataArray {

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
